package com.thingsgrid.device.monitor.service.impl;
import com.thingsgrid.device.entity.Device;
import com.thingsgrid.device.entity.PerformanceMetrics;
import com.thingsgrid.device.entity.PerformanceMetricsReport;
import com.thingsgrid.device.monitor.service.IPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class PropertyServiceImpl implements IPropertyService {

	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 *
	 *通过产品Id查询相关设备
	 * @author 陈学栋
	 * @date 2019/8/21 9:14
	 */
	@Override
	public List<Device> findDeviceById(String productId) {
		Query query = new Query();
		query.addCriteria(Criteria.where("productId").is(productId));
		List<Device> deviceInfoList = mongoTemplate.find(query, Device.class, "device");
		return deviceInfoList;
	}

	@Override
	public LinkedHashMap<String, Double> getDeviceIndex(String deviceId, Integer selectTime,String sign) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - selectTime);
		SimpleDateFormat format  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		LinkedHashMap<String, Double> hashMap = new LinkedHashMap<>();
		//查询的开始时间
		String startTime = format .format(calendar.getTime());
		for ( int i = 0; i < selectTime * 12; i++ ){
			Date date = null;
			try {
				date = format.parse(startTime);
			} catch (Exception ex)
			{
				ex.printStackTrace();
			}
			//开始查询后的五分钟
			calendar.setTime(date);
			calendar.add(Calendar.MINUTE, 5);
			String nextTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
			Query query =new Query();
			//条件查询记录
			query.addCriteria(Criteria.where("deviceId").is(deviceId).and("createTime").gte(startTime).lte(nextTime));
			List<PerformanceMetricsReport> performanceReportList = mongoTemplate.find(query, PerformanceMetricsReport.class, "performance_metrics_report");
			//查询出的总记录数
			int performanceReportSize = performanceReportList.size();
			double deviceCpu = 0f;
			double deviceMemory = 0f;
			double deviceNetwork = 0f;
			double deviceSave = 0f;
			//计算相应的值
			for (int j = 0; j<performanceReportList.size();j++){
				PerformanceMetricsReport report = performanceReportList.get(j);
				//计算cpu的值
				double reportCpu = report.getReportCpu();
				deviceCpu+=reportCpu;
				double reportMemory = report.getReportMemory();
				deviceMemory+=reportMemory;
				double reportNetwork = report.getReportNetwork();
				deviceNetwork+=reportNetwork;
				double reportSave = report.getReportSave();
				deviceSave+=reportSave;
			}
			double avgCpu = 0;
			double avgMemory = 0;
			double avgNetwork = 0;
			double avgSave = 0;
			if(performanceReportSize == 0){
				avgCpu = 0;
				avgMemory = 0;
				avgNetwork = 0;
				avgSave = 0;
			}else{
				avgCpu = deviceCpu/performanceReportSize;
				avgMemory = deviceMemory/performanceReportSize;
				avgNetwork = deviceNetwork/performanceReportSize;
				avgSave = deviceSave/performanceReportSize;
			}
			startTime = nextTime;
			//切割时间
			String[] time = startTime.split(" ");
			String pointTime = time[1].substring(0, time[1].lastIndexOf(":"));
			if(sign.equals("cpu")){
				hashMap.put(pointTime,avgCpu);
			}else if(sign.equals("memory")){
				hashMap.put(pointTime,avgMemory);
			}else if(sign.equals("network")){
				hashMap.put(pointTime,avgNetwork);
			}else{
				hashMap.put(pointTime,avgSave);
			}

		}
		return hashMap;
	}


	/**
	 *
	 *通过产品Id查询指标名称
	 * @author 陈学栋
	 * @date 2019/8/27 11:03
	 */
	@Override
	public List<PerformanceMetrics> findMetricsNameById(String productId) {
		Query query = new Query();
		query.addCriteria(Criteria.where("product_Id").is(productId));
		List<PerformanceMetrics> MetricsList = mongoTemplate.find(query, PerformanceMetrics.class, "performance_metrics_report");
		return MetricsList;
	}

}
